<html>
<head><meta charset="utf-8"><title>repr(transparent) and transmute · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html">repr(transparent) and transmute</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169322604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169322604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169322604">(Jun 30 2019 at 03:26)</a>:</h4>
<p>Is <code>repr(transparent)</code> required to transmute a newtype into its base type without UB?</p>
<p>I think the answer is "yes", but would like confirmation either way.</p>



<a name="169330557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169330557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169330557">(Jun 30 2019 at 08:15)</a>:</h4>
<p>No</p>



<a name="169330605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169330605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169330605">(Jun 30 2019 at 08:16)</a>:</h4>
<p>Transparent guarantees that the wrapper type has the same layout as the type being wrapped.</p>



<a name="169330610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169330610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169330610">(Jun 30 2019 at 08:16)</a>:</h4>
<p>If you want to transmute, you only need the size to match, and for the result to be valid (you don't need the same alignment, niches, call ABI).</p>



<a name="169332348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169332348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169332348">(Jun 30 2019 at 09:18)</a>:</h4>
<p>well it's a newtype so the <code>T</code> is certainly the same</p>



<a name="169332364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169332364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169332364">(Jun 30 2019 at 09:19)</a>:</h4>
<p>do we guarantee that <code>struct NewType(T)</code> and <code>T</code> have the same size and align? I think that's <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/34" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/34">https://github.com/rust-lang/unsafe-code-guidelines/issues/34</a> and even within the UCG we didn't get consensus^^</p>



<a name="169332365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169332365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169332365">(Jun 30 2019 at 09:19)</a>:</h4>
<p>so I'd actually answer <span class="user-mention" data-user-id="224471">@Lokathor</span>'s question with "yes"</p>



<a name="169333718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169333718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169333718">(Jun 30 2019 at 10:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> the only thing that transmute requires is same size, and that the result of the transmute is _valid_. <code>repr(transparent)</code> on the wrapper gives you that, but it is not _necessary_ to obtain that. </p>
<p>AFAICT: </p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"></span>
<span class="w">  </span><span class="k">where</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">transmute</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>is always safe: the result is always valid. </p>
<p>This _might_ fail to type-check if the <code>Wrapper</code> and <code>T</code> have different sizes, but that's about it. </p>
<p>For example, this would also be always safe: </p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(align(64))]</span><span class="w"> </span><span class="k">struct</span> <span class="nc">OveralignedWrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">OveralignedWrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">T</span><span class="w"></span>
<span class="w">  </span><span class="k">where</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">OveralignedWrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">transmute</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169335127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169335127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169335127">(Jun 30 2019 at 10:52)</a>:</h4>
<p>okay with the size checks you are basically constraining the layout</p>



<a name="169335130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169335130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169335130">(Jun 30 2019 at 10:52)</a>:</h4>
<p>did we agree in the UCG discussions that field offsets are the only "moving piece" in struct layout?</p>



<a name="169335133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169335133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169335133">(Jun 30 2019 at 10:52)</a>:</h4>
<p>that's what you are assuming here, basically</p>



<a name="169344840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169344840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169344840">(Jun 30 2019 at 15:58)</a>:</h4>
<p>Doesn't niches play in to validity?</p>



<a name="169344847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169344847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169344847">(Jun 30 2019 at 15:58)</a>:</h4>
<p>they do. or rather the other way around.</p>



<a name="169344849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169344849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169344849">(Jun 30 2019 at 15:58)</a>:</h4>
<p>validity "justifies" niches.</p>



<a name="169344877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169344877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169344877">(Jun 30 2019 at 16:00)</a>:</h4>
<p>The current stand sounds like layout is going to be: size, alignment, niches, field offsets, and call ABI <br>
For the transmute, the alignment and the call ABI are irrelevant. The size is constrained. So that leaves niches and field offsets.</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">w</span>: <span class="nc">Wrapper</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">w</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>requires the field offsets of <code>T</code> and <code>Wrapper&lt;T&gt;(T)</code> to match under the constraint that both have the same size. Also, validity requires the <code>T</code> in the <code>Wrapper</code> to always be valid. The niches of both are also the same. A more interesting wrapper can have more niches than T: e.g. <code>NonZero&lt;T&gt;</code>, or <code>Option&lt;T&gt;</code>, where niches play a role.</p>



<a name="169356532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169356532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169356532">(Jun 30 2019 at 22:25)</a>:</h4>
<p>those two lines there don't <em>necessarily</em> assert that the address of &amp;w and &amp;w.0 are the same, do they?</p>
<p>Also, one thing of note, a literal <code>transmute</code> of course ignores alignment because it gets pushed onto the stack at an aligned position of the input type and returns at an aligned position of the output type, but people often type pun references/pointers as well, so alignment still needs to be kept in mind <em>in general</em> when talking about type punning. Bit of my fault there for not being clear about the full picture on that one.</p>



<a name="169356632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169356632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169356632">(Jun 30 2019 at 22:28)</a>:</h4>
<p>Also, <code>NonNull&lt;T&gt;</code> isn't actually wrapping a <code>T</code> so it doesn't really affect niche calculation, and <code>Option&lt;T&gt;</code> will never have <em>more</em> niches than <code>T</code>, it'll always have "some number less" than <code>T</code> itself does, right?</p>



<a name="169356692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169356692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169356692">(Jun 30 2019 at 22:30)</a>:</h4>
<p>It'll have <code>|niches(Option&lt;T&gt;)| ≤ |niches(T)|</code></p>



<a name="169356702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169356702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169356702">(Jun 30 2019 at 22:31)</a>:</h4>
<p>I think it's likely that <code>0 ≤ |niches(T)| - |niches(Option&lt;T&gt;)| ≤ 1</code></p>



<a name="169356755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169356755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169356755">(Jun 30 2019 at 22:33)</a>:</h4>
<p>that second line is hard to parse</p>



<a name="169362574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169362574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169362574">(Jul 01 2019 at 01:48)</a>:</h4>
<p>"the number of niches in <code>Option&lt;T&gt;</code> is either the same as, or one less than, the number of niches in <code>T</code>"</p>



<a name="169370238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169370238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169370238">(Jul 01 2019 at 05:54)</a>:</h4>
<blockquote>
<p>Also, NonNull&lt;T&gt; isn't actually wrapping a T so it doesn't really affect niche calculation</p>
</blockquote>
<p>Which is why I said <code>NonZero&lt;T&gt;</code>, and not <code>NonNull&lt;T&gt;</code>.</p>
<blockquote>
<p>but people often type pun references/pointers as well, so alignment still needs to be kept in mind in general when talking about type punning. </p>
</blockquote>
<p>Even when type punning, it is ok to have a T at an unaligned memory location, that's what <code>repr(packed)</code> does.</p>



<a name="169370333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169370333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169370333">(Jul 01 2019 at 05:57)</a>:</h4>
<p>When the niches of the types in the transmute do not match, the only thing you need to make sure of, is not creating an invalid value. So, e.g., you can always transmute a <code>T</code> into an <code>Option&lt;T&gt;</code>, but you can't always transmute an <code>Option&lt;T&gt;</code> into a <code>T</code>.</p>



<a name="169370378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169370378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169370378">(Jul 01 2019 at 05:58)</a>:</h4>
<p>You can transmute a <code>NonZero&lt;T&gt;</code> into a <code>T</code>, but you can't transmute all <code>T</code>s into a <code>NonZero&lt;T&gt;</code>, etc.</p>



<a name="169370468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169370468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169370468">(Jul 01 2019 at 06:01)</a>:</h4>
<p>To write a safe wrapper that transmutes from <code>T</code> into <code>NonZero&lt;T&gt;</code>, you'd need to make sure that <code>T</code> is valid at <code>NonZero&lt;T&gt;</code>, e.g., by checking whether <code>T</code> is zero. Alternatively, you can transmute a <code>T</code> into a <code>Option&lt;NonZero&lt;T&gt;&gt;</code>, because that <code>Option</code> is always valid.</p>



<a name="169370563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169370563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169370563">(Jul 01 2019 at 06:03)</a>:</h4>
<p>When you use union field access to read a field, you are asserting that the field is valid, so need to take these things into account.</p>



<a name="169372656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr%28transparent%29%20and%20transmute/near/169372656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/repr(transparent).20and.20transmute.html#169372656">(Jul 01 2019 at 06:58)</a>:</h4>
<p>ah! NonZero, right</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>